package com.ruoyi.safe.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.safe.domain.CommonCooperatorTeam;
import com.ruoyi.safe.domain.vo.CommonCooperatorTeamVO;
import com.ruoyi.safe.service.ICommonCooperatorTeamService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 企业队伍----->zyx
 *
 * @author buxi
 * @date 2023-12-20
 */

@RestController
@RequestMapping("/cooperator/team")
@RequiredArgsConstructor
public class CommonCooperatorTeamController extends BaseController {

    private final ICommonCooperatorTeamService commonCooperatorTeamService;

    /**
     * 查询企业队伍列表
     */
    @PreAuthorize("@ss.hasPermi('cooperator:team:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommonCooperatorTeam commonCooperatorTeam) {
        if (ObjectUtil.isNull(commonCooperatorTeam.getProjectId())) {
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<CommonCooperatorTeamVO> commonCooperatorTeamVOS = commonCooperatorTeamService.selectCommonCooperatorTeamList(commonCooperatorTeam);
        return getDataTable(commonCooperatorTeamVOS);
    }

    /**
     * 获取企业队伍详细信息
     */
    @PreAuthorize("@ss.hasPermi('cooperator:team:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(commonCooperatorTeamService.selectCommonCooperatorTeamById(id));
    }

    /**
     * 新增企业队伍
     */
    @PreAuthorize("@ss.hasPermi('cooperator:team:add')")
    @Log(title = "企业班组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody CommonCooperatorTeam commonCooperatorTeam) {
        return toAjax(commonCooperatorTeamService.insertCommonCooperatorTeam(commonCooperatorTeam));
    }

    /**
     * 修改企业队伍
     */
    @PreAuthorize("@ss.hasPermi('cooperator:team:edit')")
    @Log(title = "企业班组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody CommonCooperatorTeam commonCooperatorTeam) {
        return toAjax(commonCooperatorTeamService.updateCommonCooperatorTeam(commonCooperatorTeam));
    }

    /**
     * 删除企业队伍
     */
    @PreAuthorize("@ss.hasPermi('cooperator:team:remove')")
    @Log(title = "企业班组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commonCooperatorTeamService.deleteCommonCooperatorTeamByIds(ids));
    }

    /**
     * 根据项目id查询队伍列表
     */
    @GetMapping("/selectTeamByProjectId/{projectId}")
    public AjaxResult selectTeamByProjectId(@PathVariable("projectId") Long projectId) {

        List<CommonCooperatorTeam> commonCooperatorTeams = commonCooperatorTeamService.selectCommonCooperatorTeamListByProjectId(projectId);

        return success(commonCooperatorTeams);
    }
}
